﻿using System;
using System.Windows;
using System.Windows.Controls;

using Microsoft.Xna.Framework.Input;

namespace Microsoft.Xna.Framework
{
    internal class SilverlightGameWindow : GameWindow
    {
		internal Image _root;
		private string screenDeviceName;
		private bool inTransition;
		private bool willBeFullScreen;

		#region Properties
		public override bool AllowUserResizing { get; set; }

		public override Rectangle ClientBounds
		{
			get { return new Rectangle(0, 0, (int)this._root.Width, (int)this._root.Height); }
		}

		public override IntPtr Handle
		{
			get { return IntPtr.Zero; }
		}

		public override string ScreenDeviceName
		{
			get { return screenDeviceName; }
		}

		public override DisplayOrientation CurrentOrientation
		{
			get { return DisplayOrientation.Default; }
		}
		#endregion

		public SilverlightGameWindow()
            : base()
        {
			this._root = new Image();
			this._root.HorizontalAlignment = HorizontalAlignment.Stretch;
			this._root.VerticalAlignment = VerticalAlignment.Stretch;
			Application.Current.RootVisual = this._root;
			Application.Current.RootVisual.GotFocus += new RoutedEventHandler(RootVisual_GotFocus);
			Application.Current.RootVisual.LostFocus += new RoutedEventHandler(RootVisual_LostFocus);
			Keyboard.Initialize();
			Mouse.Initialize();
        }

		void RootVisual_LostFocus(object sender, RoutedEventArgs e)
		{
			base.OnDeactivated();
		}

		void RootVisual_GotFocus(object sender, RoutedEventArgs e)
		{
			base.OnActivated();
		}

        public override void BeginScreenDeviceChange(bool willBeFullScreen)
        {
			inTransition = true;
			this.willBeFullScreen = willBeFullScreen; 
        }

        public override void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
        {
			this.screenDeviceName = screenDeviceName;

			Application.Current.Host.Content.IsFullScreen = this.willBeFullScreen;

			//this._root.Width = clientWidth;
			//this._root.Height = clientHeight;

			OnClientSizeChanged();

			inTransition = false;
        }

        protected override void SetTitle(string title)
        {
        }

		protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
		{
			
		}
	}
}
